// pages/camel_card/my_camelCard_find_password/index.js
var time_box;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pay_password: '',
    // 60秒倒计时
    times: '',
    pay_password: '',//支付密码
    confirm_password: '',//确认密码
    phone_password: '',//手机号
    verification_password: '',//验证码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (o) {
    console.log(o.type) 
    var type = typeof (o.type) == 'undefined' ? 'set' : o.type
     wx.setNavigationBarTitle({
      title: type=='set'?'设置支付密码':'找回支付密码',
    })

  },
  //支付密码：
  enter_input:function(e){
    var that=this;
    console.log(e);
    if (e.currentTarget.dataset.name == "pay_password") {
      that.setData({ pay_password: e.detail.value });
    }
    if (e.currentTarget.dataset.name == "confirm_password") {
      that.setData({ confirm_password: e.detail.value });
    }
    if (e.currentTarget.dataset.name == "phone_password") {
      that.setData({ phone_password: e.detail.value });
    }
    if (e.currentTarget.dataset.name == "verification_password") {
      that.setData({ verification_password: e.detail.value });
    }
  },
  //清除用户输入
  delete_icon: function (e) {
    var that = this;
    if (e.currentTarget.dataset.name == "pay_password") {
      that.setData({ pay_password: '' });
    }
    if (e.currentTarget.dataset.name == "confirm_password") {
      that.setData({ confirm_password: '' });
    } 
    if (e.currentTarget.dataset.name == "phone_password") {
      that.setData({ phone_password: '' });
    }
    if (e.currentTarget.dataset.name == "verification_password") {
      that.setData({ verification_password: '' });
    }
  },
  // 密码对比
  blur_input:function(){
    var that=this;
    if (that.data.pay_password != that.data.confirm_password && that.data.pay_password != '' && that.data.confirm_password !=''){
      prompt('两次密码有误', 'loading', false);
    }
  },
  // 用户点击获取验证码
  verification: function () {
    var that = this;
    if (that.data.pay_password == '' || that.data.confirm_password == '' ) {
      prompt('请输入密码', 'loading', false); return;
    }
    var phone=that.data.phone_password;
    console.log(phone);
    if (!(/^1\d{10}$/.test(phone))) {
      prompt('手机号不正确', 'loading', false); return;
    }
    if (that.data.times == '') {
      that.setData({ times: 60, })
      getLocalTime(that, that.data.times);
      that.onShow();
       wx.request({
                url: app.url + 'api_ucenter/send_sms',
                data: {
                    mobile: phone,
                    type: 'setPayPasswd',
                },
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token_key')
                }, success: function (json) {
                  console.log(json)
                  if(json.data.done==false){
                     prompt(json.data.message, 'loading', true);
                  }
                }
            })
    } else {
      prompt('请稍后点击', 'loading', true);
    }
  },
  // 表单提交
  formSubmit: function (e) {
    var that = this;
    console.log(e);

    /*if (that.data.pay_password.length>6) {
      prompt('密码位数错误', 'loading', false); return;
    }*/
    if (e.detail.value.confirm_password != e.detail.value.pay_password) {
      prompt('两次密码有误', 'loading', false); return;
    }
    if (!(/^1\d{10}$/.test(e.detail.value.phone_password))) {
      prompt('手机号错误', 'loading', false); return;
    }
    if (that.data.times == '') {
      prompt('请获取验证码', 'loading', false); return;
    }
    if (e.detail.value.verification_password == '') {
      prompt('请输入验证码', 'loading', false); return;
    }
 
   wx.request({
                url: app.url + 'api_ucenter/setPayPasswd',
                data: {
                    phoneNumber: that.data.phone_password,
                    passwd: that.data.pay_password,
                    confirm_password: that.data.confirm_password,
                    smsCode: e.detail.value.verification_password,
                },
                header: {
                    'content-type': 'application/json',
                    'token': wx.getStorageSync('token_key')
                }, success: function (json) {
                console.log(json)
                  if(json.data.done==true){
                   that.setData({ modal_box: true, modal_types: 1, })
                       prompt('修改成功', 'success', false);
                       setTimeout(() => { wx.redirectTo({ url: '/pages/camel_card/my_camelCard/index', }) }, 1200)
                  }else{
                     prompt(json.data.message, 'loading', true);
                  }
             }   
            })

    // wx.request({
    //   url: '',
    //   data: '',
    //   header: {},
    //   method: GET,
    //   dataType: json,
    //   responseType: text,
    //   success: function(res) {

    // 伪数据

    //   },
    //   fail: function(res) {},
    //   complete: function(res) {},
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (typeof that.data.times == 'number') {
      time_box = setInterval(function () {
        getLocalTime(that, that.data.times)
      }, 1000)
    }
  },
})

// 提示
function prompt(title, status, mask) {
  wx.showToast({
    title: title,
    icon: status,
    duration: 1200,
    mask: mask
  })
}


// 获取验证码倒计时
function getLocalTime(that, nS) {
  console.log(5555);
  if (nS != 0) {
    --nS;
    var S;
    function fillZero(v) {
      if (v < 10) {
        v = '0' + v;
      }
      return v;
    }

    var d = new Date(parseInt(nS) * 1000);
    S = fillZero(d.getSeconds());
    that.setData({ times: S, })
  } else {
    that.setData({ times: '', })
    clearInterval(time_box);
    time_box = '';
  }
}